<?php
/**
 * 重庆柯一网络有限公司
 * 遵循MT协议，开源并可商业使用，没有任何限制
 * @Author:cqkyi
 * @Date: 2020/9/14 12:10
 * 官方惟一地址：www.cqkyi.com
 */

namespace app\common\model\admin;


use app\common\utils\Res;
use app\common\validate\MenuValidate;
use think\Model;

class MenuModel extends Model
{

    protected $name="sys_menu";

    protected $pk="menu_id";

    /**
     * @param $username 用户名来查询用户拥有的菜单
     * 查询菜单
     */


    /**
     * @param $username 不是超级管理员的管理员
     * @return mixed
     */
    public static function userMenuByUserName($username)
    {
        return self::alias('m')
            ->join('ky_sys_role_menu rm','rm.menu_id = m.menu_id')
            ->join('sys_user_role ur','rm.role_id = ur.role_id')
            ->join('ky_sys_role ro','ur.role_id = ro.role_id')
            ->join('sys_user u','ur.user_id = u.user_id')
            ->where('u.username',$username)
            ->where('m.status',0)
            ->where('m.menu_type','in','0,1')
            ->select();
    }

    /**
     * @return mixed
     * 超级管理员的权限
     */
    public static function adminMenuByUserName()
    {
        return self::alias('m')->where('status',0)->where('menu_type','in','0,1')->select();
    }

    /**
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 管理员的所有权限
     */
    public static function adminPerms(){
        return self::alias('m')->where('status',0)->select();
    }


    public static function userPermsByUserName($username)
    {
        return self::alias('m')
            ->join('ky_sys_role_menu rm','rm.menu_id = m.menu_id')
            ->join('sys_user_role ur','rm.role_id = ur.role_id')
            ->join('ky_sys_role ro','ur.role_id = ro.role_id')
            ->join('sys_user u','ur.user_id = u.user_id')
            ->where('u.username',$username)
            ->where('m.status',0)
            ->select();
    }


    /**
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 查询所有
     */
    public static function findByAll()
    {
        return self::order('order_num asc')->select();
    }

    /**
     * 查询所有菜单及目录
     */
    public static function findByType()
    {
        return self::where('menu_type','in','0,1')->select();
    }

    /**
     * @param $param 接收的参数
     * @return mixed|\think\response\Json
     */
    public static function add($param)
    {
        try {
            $validate = new MenuValidate();
            if (!$validate->check($param)) {
                return Res::ModelMsg($validate->getError());
            }
            $menuModel = new MenuModel();
            $menuModel->save($param);
            return Res::Modelok("添加成功");
        }catch (\PDOException $exception){
            return Res::ModelMsg($exception->getMessage());
        }
    }

    /**
     * @param $param
     * @return array|mixed
     */
    public static function edit($param)
    {
        try {
            $validate = new MenuValidate();
            if (!$validate->check($param)) {
                return Res::ModelMsg($validate->getError());
            }
            self::update($param, ['menu_id' => $param['menu_id']]);
            return Res::Modelok("修改成功");
        }catch (\PDOException $exception){
            return Res::ModelMsg($exception->getMessage());
        }
    }

    /**
     * @param $id
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function del($id)
    {
        try{
            $res = self::find($id);
            $list = self::where('parent_id',$id)->find();
            if($list){
                return Res::ModelMsg('该菜单下有子节点，请先删除子节点后再进行相关操作！');
            }
            if(empty($res)){
                return Res::ModelMsg('你要删除的内容不存在');
            }
            self::destroy($id);
            return Res::Modelok('删除成功');
        }catch (PDOException $e){
            return Res::ModelMsg($e->getMessage());
        }
    }


    public static function changeStatus($id)
    {
        try{
        $result = self::find($id);
        $status= $result['status']==0?1:0;
        self::where('menu_id',$id)->update(['status'=>$status]);
        return Res::Modelok('修改成功');
       }catch (\PDOException $e){
            return Res::ModelMsg($e->getMessage());
       }

    }

    public static function changeOrder($param)
    {
        try{
           self::where('menu_id',$param['id'])->update(['order_num'=>$param['num']]);
            return Res::Modelok('修改成功');
        }catch (\PDOException $e){
            return Res::ModelMsg($e->getMessage());
        }
    }

}